<?php
	class Search {
		static function globalSearch($keyword){
			$keyword = "%".$keyword."%";
			$result = array(
				"destination" => 0,
				"hotel" => 0,
				"random" => 0,
				"value" => ""
			);
			
			$sql_destination = @mysql_query("
				SELECT
				destinations.slug
				FROM
				destinations
				WHERE
				destinations.destination_name LIKE '$keyword'
				LIMIT 0, 1
			");
			
			$arr_destinations = @mysql_fetch_array($sql_destination);
			$count_destination = @mysql_num_rows($sql_destination);
			
			if(!empty($count_destination)){
				$slug = $arr_destinations['slug'];
				$sql = @mysql_query("SELECT
						destinations.id AS destination_id,
						destinations.destination_name,
						hotels.id,
						hotels.agoda_id,
						hotels.hotel_name,
						hotels.hotel_slug,
						hotels.photo1,
						hotels.addressline1,
						hotels.zipcode,
						hotels.star_rating,
						hotels.overview,
						hotels.url,
						hotels.rates_from,
						hotels.rates_currency,
						hotels.number_of_reviews,
						hotels.rating_average
						FROM
						destinations
						Inner Join hotels ON destinations.id = hotels.destination_id
						WHERE
						destinations.slug = '$slug'
						LIMIT 0, 20
				");
				
				$result['destination'] = 1;
				$result['value'] = $sql;				
			} else {
				$sql = @mysql_query("SELECT
						destinations.id AS destination_id,
						destinations.destination_name,
						hotels.id AS hotel_id,
						hotels.agoda_id,
						hotels.hotel_name,
						hotels.hotel_slug,
						hotels.addressline1,
						hotels.zipcode,
						hotels.star_rating,
						hotels.longitude,
						hotels.latitude,
						hotels.url,
						hotels.checkin,
						hotels.checkout,
						hotels.numberrooms,
						hotels.numberfloors,
						hotels.yearopened,
						hotels.yearrenovated,
						hotels.photo1,
						hotels.photo2,
						hotels.photo3,
						hotels.photo4,
						hotels.photo5,
						hotels.overview,
						hotels.rates_from,
						hotels.rates_currency,
						hotels.number_of_reviews,
						hotels.rating_average
						FROM
						destinations
						Inner Join hotels ON destinations.id = hotels.destination_id
						WHERE
						hotels.hotel_name like '$keyword'
						LIMIT 0, 20
				");
				
				$count_hotels = @mysql_num_rows($sql);
				
				if($count_hotels == 1){
					$result['hotel'] = 1;
					$result['value'] = @mysql_fetch_array($sql);;
				} else if($count_hotels > 1){
					$result['destination'] = 1;
					$result['value'] = $sql;
				}
			}
			
			return $result;
		}
	}
?>